package com.kzd.sdk.core;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.kzd.sdk.callback.KZD_OnLoginListener;
import com.kzd.sdk.callback.KZD_OnPayListener;
import com.kzd.sdk.callback.KZD_OnRoleListener;
import com.kzd.sdk.entity.KZD_SdkInitParams;
import com.kzd.sdk.entity.KZD_SdkPayParams;
import com.kzd.sdk.entity.KZD_SdkRoleParams;

public class KZD_SdkManager {

    private static class Holder {
        private static final KZD_SdkController instance = new KZD_SdkController();
    }

    private static KZD_SdkController getInstance() {
        return KZD_SdkManager.Holder.instance;
    }


    public static void init(Application application, KZD_SdkInitParams params) {
        if (application == null) {
            throw new NullPointerException("KZD_SDK: Context can't be null.");
        }
        if (params == null) {
            throw new NullPointerException("KZD_SDK: KZD_SdkInitParams can't be null.");
        }
        getInstance().init(application, params);
    }

    public static void setListener(KZD_OnLoginListener loginListener, KZD_OnPayListener payListener, KZD_OnRoleListener roleListener) {
        setLoginListener(loginListener);
        setPayListener(payListener);
        setRoleListener(roleListener);
    }

    public static void setLoginListener(KZD_OnLoginListener loginListener) {
        if (loginListener == null) {
            throw new NullPointerException("KZD_SDK: KZD_OnLoginListener can't be null.");
        }
        getInstance().setOnLoginListener(loginListener);
    }

    public static void setPayListener(KZD_OnPayListener payListener) {
        if (payListener == null) {
            throw new NullPointerException("KZD_SDK: KZD_OnPayListener can't be null.");
        }
        getInstance().setOnPayListener(payListener);
    }

    public static void setRoleListener(KZD_OnRoleListener roleListener) {
        if (roleListener == null) {
            throw new NullPointerException("KZD_SDK: KZD_OnRoleListener can't be null.");
        }
        getInstance().setOnRolesListener(roleListener);
    }

    public static void login(Activity activity, KZD_OnLoginListener loginListener) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        if (getInstance().isNotLoginListener()) {
            throw new NullPointerException("KZD_SDK: KZD_OnLoginListener can't be null.");
        }
        getInstance().login(activity, loginListener);
    }

    public static void login(Activity activity) {
        login(activity, null);
    }

    public static void paying(Activity activity, KZD_SdkPayParams payParams, KZD_OnPayListener payListener) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        if (payParams == null) {
            throw new NullPointerException("KZD_SDK: KZD_SdkPayParams can't be null.");
        }
        if (getInstance().isNotPayListener()) {
            throw new NullPointerException("KZD_SDK: KZD_OnPayingListener can't be null.");
        }
        getInstance().paying(activity, payParams, payListener);
    }

    public static void paying(Activity activity, KZD_SdkPayParams payParams) {
        paying(activity, payParams, null);
    }

    public static void roles(Activity activity, KZD_SdkRoleParams roleParams, KZD_OnRoleListener roleListener) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        if (roleParams == null) {
            throw new NullPointerException("KZD_SDK: KZD_SdkRoleParams can't be null.");
        }
        if (getInstance().isNotRolesListener()) {
            throw new NullPointerException("KZD_SDK: KZD_OnRolesListener can't be null.");
        }
        getInstance().roles(activity, roleParams, roleListener);
    }

    public static void roles(Activity activity, KZD_SdkRoleParams roleParams) {
        roles(activity, roleParams, null);
    }

    public static void openFloatView(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        if (getInstance().isNotLogin()) {
            throw new NullPointerException("KZD_SDK: Not logged in.");
        }
        getInstance().openFloatView(activity);
    }

    public static void closeFloatView(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().closeFloatView(activity);
    }

    public static void exit(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().exit(activity);
    }

    public static boolean isNotLogin() {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        return getInstance().isNotLogin();
    }

    public static void onCreate(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onCreate(activity);
    }

    public static void onStart(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onStart(activity);
    }

    public static void onResume(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onResume(activity);
    }

    public static void onPause(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onPause(activity);
    }

    public static void onStop(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onStop(activity);
    }

    public static void onRestart(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onRestart(activity);
    }

    public static void onDestroy(Activity activity) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onDestroy(activity);
    }

    public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (getInstance().isNotInit()) {
            throw new RuntimeException("KZD_SDK: Sdk is not initialized,Call 'KZD_SdkManager.init()' in the application");
        }
        if (activity == null) {
            throw new NullPointerException("KZD_SDK: Activity can't be null.");
        }
        getInstance().onActivityResult(activity, requestCode, resultCode, data);
    }

}
